package com.cloudcc.mobile.view.crop;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.widget.Toast;

/**
 * Builder for crop Intents and utils for handling result
 */
public class Crop {

	/**
	 * 裁剪
	 */
	public static final int REQUEST_CROP = 6709;
	/**
	 * 选取图片
	 */
	public static final int REQUEST_PICK = 9162;
	public static final int RESULT_ERROR = 404;

	static interface Extra {
		String ASPECT_X = "aspect_x";
		String ASPECT_Y = "aspect_y";
		String MAX_X = "max_x";
		String MAX_Y = "max_y";
		String ERROR = "error";
	}

	private Intent cropIntent;

	/**
	 * Create a crop Intent builder with source image
	 * 
	 * @param source
	 *            Source image URI
	 */
	public Crop(Uri source) {
		cropIntent = new Intent();
		cropIntent.setData(source);
	}

	/**
	 * Set output URI where the cropped image will be saved
	 * 
	 * @param output
	 *            Output image URI
	 */
	public Crop output(Uri output) {
		cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, output);
		return this;
	}

	/**
	 * Set fixed aspect ratio for crop area
	 * 
	 * @param x
	 *            Aspect X
	 * @param y
	 *            Aspect Y
	 */
	public Crop withAspect(int x, int y) {
		cropIntent.putExtra(Extra.ASPECT_X, x);
		cropIntent.putExtra(Extra.ASPECT_Y, y);
		return this;
	}

	/**
	 * Crop area with fixed 1:1 aspect ratio
	 */
	public Crop asSquare() {
		cropIntent.putExtra(Extra.ASPECT_X, 1);
		cropIntent.putExtra(Extra.ASPECT_Y, 1);
		return this;
	}

	/**
	 * Set maximum crop size
	 * 
	 * @param width
	 *            Max width
	 * @param height
	 *            Max height
	 */
	public Crop withMaxSize(int width, int height) {
		cropIntent.putExtra(Extra.MAX_X, width);
		cropIntent.putExtra(Extra.MAX_Y, height);
		return this;
	}

	/**
	 * 发送一个 剪切的intent
	 * 
	 * @param activity
	 *            Activity to receive result
	 */
	public void start(Activity activity) {
		start(activity, REQUEST_CROP);
	}

	/**
	 * Send the crop Intent with a custom requestCode
	 * 
	 * @param activity
	 *            Activity to receive result
	 * @param requestCode
	 *            requestCode for result
	 */
	public void start(Activity activity, int requestCode) {
		activity.startActivityForResult(getIntent(activity), requestCode);
	}

	public void start(android.support.v4.app.Fragment fragment) {
		fragment.startActivityForResult(getIntent(fragment.getActivity()),
				REQUEST_CROP);
	}

	/**
	 * Send the crop Intent!
	 * 
	 * @param context
	 *            Context
	 * @param fragment
	 *            Fragment to receive result
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public void start(Context context, Fragment fragment) {
		start(context, fragment, REQUEST_CROP);
	}

	/**
	 * Send the crop Intent with a custom requestCode
	 * 
	 * @param context
	 *            Context
	 * @param fragment
	 *            Fragment to receive result
	 * @param requestCode
	 *            requestCode for result
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public void start(Context context, Fragment fragment, int requestCode) {
		fragment.startActivityForResult(getIntent(context), requestCode);
	}

	/**
	 * Get Intent to start crop Activity
	 * 
	 * @param context
	 *            Context
	 * @return Intent for CropImageActivity
	 */
	public Intent getIntent(Context context) {
		cropIntent.setClass(context, CropImageActivity.class);
		return cropIntent;
	}

	/**
	 * Retrieve URI for cropped image, as set in the Intent builder
	 * 
	 * @param result
	 *            Output Image URI
	 */
	public static Uri getOutput(Intent result) {
		return result.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
	}

	/**
	 * Retrieve error that caused crop to fail
	 * 
	 * @param result
	 *            Result Intent
	 * @return Throwable handled in CropImageActivity
	 */
	public static Throwable getError(Intent result) {
		return (Throwable) result.getSerializableExtra(Extra.ERROR);
	}

	/**
	 * 进入相册选择图片 默认请求吗
	 * 
	 * @param activity
	 *            Activity that will receive result
	 */
	public static void pickImage(Activity activity) {
		pickImage(activity, REQUEST_PICK);
	}

	/**
	 * 进入相册选择图片
	 * 
	 * @param 将要返回的activity
	 * @param 请求码
	 */
	public static void pickImage(Activity activity, int requestCode) {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT)
				.setType("image/*");
		try {
			activity.startActivityForResult(intent, requestCode);
		} catch (ActivityNotFoundException e) {
			Toast.makeText(activity, "error", Toast.LENGTH_SHORT).show();
		}
	}

}
